<?php require 'common/head.php'; ?>
<?php
    require '../db_connection.php';
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name    = $_POST['project_name'];
        $desc    = $_POST['description'];
        $start_d = $_POST['start_date'];
        $end_d   = $_POST['end_date'];
        $t_amount   = $_POST['target_amount'];
        $c_amount   = $_POST['collected_amount'];
        $status   = $_POST['status'];

        $sql = "INSERT INTO `donation_projects` (`project_name`, `description`, `start_date`, `end_date`, `target_amount`, `collected_amount`, `status`)
             VALUES ('$name', '$desc', '$start_d', '$end_d', '$t_amount', '$c_amount', $status)";
        if ($conn->query($sql) === TRUE) {
            echo "<script>alert('添加成功');location.href='donation_project_list.php';</script>";
        } else {
            echo "<script>alert('添加失败');history.back();</script>";
        }
        $conn->close();
        die();
    }
    $id   = $_GET['id'];
    $sql    = "SELECT * FROM donation_projects WHERE id=$id";
    $result = $conn->query($sql);
    $info   = [];
    if ($result) {
      $info = $result->fetch_assoc();
      $result->free();
    }
    
    $conn->close();
?>
</head>
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>添加项目</strong></div>
  <div class="body-content">
  <form method="post" class="form-x" action="donation_project_add.php" style="margin-top:20px;" enctype="multipart/form-data">  
      <div class="form-group">
        <div class="label">
          <label>项目名称</label>
        </div>
        <div class="field">
          <input type="text" class="input" value="<?php echo $info['project_name']; ?>" name="project_name" data-validate="required:请输入项目名称" />
          <input type="hidden" value="<?php echo $info['id']; ?>" name="id" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>开始日期</label>
        </div>
        <div class="field">
          <input type="date" class="input" value="<?php echo $info['start_date']; ?>" name="start_date" data-validate="required:请输入开始日期" />
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>结束日期</label>
        </div>
        <div class="field">
          <input type="date" class="input" value="<?php echo $info['end_date']; ?>" name="end_date" data-validate="required:请输入结束日期" />
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>目标金额</label>
        </div>
        <div class="field">
          <input type="text" class="input" value="<?php echo $info['target_amount']; ?>" name="target_amount" data-validate="required:请输入目标金额" />
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>已筹金额</label>
        </div>
        <div class="field">
          <input type="text" class="input" value="<?php echo $info['collected_amount']; ?>" name="collected_amount" data-validate="required:请输入已筹金额" />
        </div>
      </div>
      <div class="form-group" style="display:flex;align-items:center;">
        <div class="label">
          <label>状态</label>
        </div>
        <div class="field">
            <label>进行中 <input type="radio" value="1" name="status" <?php if ($info['status'] == 1) echo 'checked'; ?>/></label>
            <label>已完成 <input type="radio" value="2" name="status" <?php if ($info['status'] == 2) echo 'checked'; ?>/></label>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>描述</label>
        </div>
        <div class="field">
          <textarea class="input" name="description" style="height:90px;"><?php echo $info['description']; ?></textarea>
        </div>
      </div> 
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <button class="button bg-main" type="submit"> 提交</button>
        </div>
      </div>    
    </form>
  </div>
</div>

</body></html>   